home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / RTF / test / rt_test.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  149 lines

  1. /* rt_test.c
  2.  *
  3.  * Test driver for RichText widget.
  4.  */
  5.  
  6. #include <Xm/Xm.h>
  7. #include <Xm/ScrolledW.h>
  8. #include <Xm/FileSB.h>
  9.  
  10. #include "RichText.h"
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14.  
  15. static void
  16. clickCB(Widget caller, XtPointer dummy, XtPointer cbd)
  17. {
  18.   XcRichTextCallbackStruct* rt = (XcRichTextCallbackStruct*)cbd;
  19.  
  20.   printf("selection: %.*s\n", rt->point - rt->mark, rt->value + rt->mark);
  21.   printf("next 10 chars: %.10s\n", rt->value + rt->point);
  22.  
  23.   if(rt->point == rt->mark){
  24.     Boolean rtf;
  25.     XtVaGetValues(caller, XcNrtf, &rtf, NULL);
  26.     printf("RichText interpretation: %s\n", rtf ? "off" : "on");
  27.     XtVaSetValues(caller, XcNrtf, !rtf, NULL);
  28.   }
  29. }
  30.  
  31.  
  32. static void resizeHandler(Widget w, XtPointer pt, XtPointer motifdata)
  33. {
  34.   Widget rt = (Widget)pt;
  35.   XmDrawingAreaCallbackStruct* das = (XmDrawingAreaCallbackStruct*)motifdata;
  36.   Dimension ow, width;
  37.  
  38.   XtVaGetValues(w,
  39.         XmNwidth, &width,
  40.         NULL);
  41.  
  42.   /* ignore sizes that are way too small */
  43.   if(width > 100)
  44.     XtVaSetValues(rt,
  45.           XmNwidth, width - 15,
  46.           NULL);
  47. }
  48.  
  49. static void showfile(Widget w, XtPointer pt, XtPointer md)
  50. {
  51.   Widget rt = (Widget)pt;
  52.   XmFileSelectionBoxCallbackStruct* fs = (XmFileSelectionBoxCallbackStruct*)md;
  53.   FILE* f;
  54.   struct stat s;
  55.   char* text;
  56.   char* fname;
  57.  
  58.   if(!XmStringGetLtoR(fs->value, XmSTRING_DEFAULT_CHARSET, &fname)){
  59.     XtWarning("can't XmStringGetLtoR");
  60.     return;
  61.   }
  62.  
  63.   if(stat(fname, &s)){
  64.     XtWarning("can't stat file");
  65.     XtFree(fname);
  66.     return;
  67.   }
  68.  
  69.   f = fopen(fname, "r");
  70.   XtFree(fname);
  71.  
  72.   if(!f){
  73.     XtWarning("%s: can't open %s\n");
  74.     return;
  75.   }
  76.  
  77.   if(!(text = (char*)XtMalloc(s.st_size + 1))){
  78.     XtWarning("%s: can't malloc enough memory for: %s\n");
  79.     return;
  80.   }
  81.   
  82.   if(fread(text, sizeof(char), s.st_size, f) != s.st_size){
  83.     XtWarning("%s: error reading for: %s\n");
  84.     XtFree(text);
  85.     return;
  86.   }
  87.  
  88.   fclose(f);
  89.   text[s.st_size] = 0;
  90.  
  91.   XtVaSetValues(rt,
  92.         XcNrtf, text[0] == '{', /* cheap hack test */
  93.         XmNvalue, text,
  94.         NULL);
  95. }
  96.  
  97.  
  98. main(argc, argv)
  99. Cardinal argc; char *argv[];
  100. {
  101.   XtAppContext app;
  102.   Widget appShell;
  103.   Widget rt, sc, clip, fsb;
  104.  
  105.   appShell = XtAppInitialize(&app, "RichTextTest",
  106.                  NULL, 0,
  107.                  &argc, argv, NULL,
  108.                  (ArgList)NULL, 0);
  109.  
  110.   fsb = XmCreateFileSelectionDialog(appShell, "filechooser",
  111.                     NULL, 0);
  112.   XtManageChild(fsb);
  113.  
  114.   sc = XtVaCreateManagedWidget("scroller", xmScrolledWindowWidgetClass,
  115.                    appShell,
  116.                    XmNscrollBarDisplayPolicy, XmAS_NEEDED,
  117.                    XmNscrollingPolicy, XmAUTOMATIC,
  118.                    NULL);
  119.  
  120.   rt = XtVaCreateManagedWidget("richtext", xcRichTextWidgetClass,
  121.                    sc,
  122.                    NULL);
  123.  
  124.   XmScrolledWindowSetAreas(sc, NULL, NULL, rt);
  125.  
  126.   /* In order to allow the user to change the width
  127.      of the RichText widget, I trap resize events
  128.      on the clip window. */
  129.   XtVaGetValues(sc,
  130.         XmNclipWindow, &clip,
  131.         NULL);
  132.   XtAddCallback(clip, XmNresizeCallback, resizeHandler, (XtPointer)rt);
  133.  
  134.  
  135.   /* show files on request */
  136.   XtAddCallback(fsb, XmNokCallback, showfile, (XtPointer)rt);
  137.  
  138.   /* provide some feedback to clicks in the text area */
  139.   XtAddCallback(rt, XmNmotionVerifyCallback, clickCB, 0);
  140.  
  141.   XtRealizeWidget ( appShell );
  142.   XtAppMainLoop ( app );
  143.   
  144.   return 0;
  145. }
  146.  
  147.  
  148.  
  149.